﻿
using System.Web.Security;

namespace GradeApp.Web.Services
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Data;
    using System.Linq;
    using System.ServiceModel.DomainServices.EntityFramework;
    using System.ServiceModel.DomainServices.Hosting;
    using System.ServiceModel.DomainServices.Server;
    using GradeApp.Web;


    // Implements application logic using the GradeAppDbEntities context.
    // TODO: Add your application logic to these methods or in additional methods.
    // TODO: Wire up authentication (Windows/ASP.NET Forms) and uncomment the following to disable anonymous access
    // Also consider adding roles to restrict access as appropriate.
    // [RequiresAuthentication]
    [EnableClientAccess()]
    public class GradeAppDomainService : LinqToEntitiesDomainService<GradeAppDbEntities>
    {

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'aspnet_Membership' query.
        public IQueryable<aspnet_Membership> GetAspnet_Membership()
        {
            return this.ObjectContext.aspnet_Membership;
        }

        public void InsertAspnet_Membership(aspnet_Membership aspnet_Membership)
        {
            if ((aspnet_Membership.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(aspnet_Membership, EntityState.Added);
            }
            else
            {
                this.ObjectContext.aspnet_Membership.AddObject(aspnet_Membership);
            }
        }

        public void UpdateAspnet_Membership(aspnet_Membership currentaspnet_Membership)
        {
            this.ObjectContext.aspnet_Membership.AttachAsModified(currentaspnet_Membership, this.ChangeSet.GetOriginal(currentaspnet_Membership));
        }

        public void DeleteAspnet_Membership(aspnet_Membership aspnet_Membership)
        {
            if ((aspnet_Membership.EntityState == EntityState.Detached))
            {
                this.ObjectContext.aspnet_Membership.Attach(aspnet_Membership);
            }
            this.ObjectContext.aspnet_Membership.DeleteObject(aspnet_Membership);
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'aspnet_Users' query.
        public IQueryable<aspnet_Users> GetAspnet_Users()
        {
            return this.ObjectContext.aspnet_Users;
        }

        public void InsertAspnet_Users(aspnet_Users aspnet_Users)
        {
            if ((aspnet_Users.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(aspnet_Users, EntityState.Added);
            }
            else
            {
                this.ObjectContext.aspnet_Users.AddObject(aspnet_Users);
            }
        }

        public void UpdateAspnet_Users(aspnet_Users currentaspnet_Users)
        {
            this.ObjectContext.aspnet_Users.AttachAsModified(currentaspnet_Users, this.ChangeSet.GetOriginal(currentaspnet_Users));
        }

        public void DeleteAspnet_Users(aspnet_Users aspnet_Users)
        {
            if ((aspnet_Users.EntityState == EntityState.Detached))
            {
                this.ObjectContext.aspnet_Users.Attach(aspnet_Users);
            }
            this.ObjectContext.aspnet_Users.DeleteObject(aspnet_Users);
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'AssesmentForms' query.
        public IQueryable<AssesmentForms> GetAssesmentForms()
        {
            return this.ObjectContext.AssesmentForms;
        }

        public void InsertAssesmentForms(AssesmentForms assesmentForms)
        {
            if ((assesmentForms.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(assesmentForms, EntityState.Added);
            }
            else
            {
                this.ObjectContext.AssesmentForms.AddObject(assesmentForms);
            }
        }

        public void UpdateAssesmentForms(AssesmentForms currentAssesmentForms)
        {
            this.ObjectContext.AssesmentForms.AttachAsModified(currentAssesmentForms, this.ChangeSet.GetOriginal(currentAssesmentForms));
        }

        public void DeleteAssesmentForms(AssesmentForms assesmentForms)
        {
            if ((assesmentForms.EntityState == EntityState.Detached))
            {
                this.ObjectContext.AssesmentForms.Attach(assesmentForms);
            }
            this.ObjectContext.AssesmentForms.DeleteObject(assesmentForms);
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'AssesmentResults' query.
        public IQueryable<AssesmentResults> GetAssesmentResults()
        {
            return this.ObjectContext.AssesmentResults;
        }

        public void InsertAssesmentResults(AssesmentResults assesmentResults)
        {
            if ((assesmentResults.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(assesmentResults, EntityState.Added);
            }
            else
            {
                this.ObjectContext.AssesmentResults.AddObject(assesmentResults);
            }
        }

        public void UpdateAssesmentResults(AssesmentResults currentAssesmentResults)
        {
            this.ObjectContext.AssesmentResults.AttachAsModified(currentAssesmentResults, this.ChangeSet.GetOriginal(currentAssesmentResults));
        }

        public void DeleteAssesmentResults(AssesmentResults assesmentResults)
        {
            if ((assesmentResults.EntityState == EntityState.Detached))
            {
                this.ObjectContext.AssesmentResults.Attach(assesmentResults);
            }
            this.ObjectContext.AssesmentResults.DeleteObject(assesmentResults);
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'Classes' query.
        public IQueryable<Classes> GetClasses()
        {
            return this.ObjectContext.Classes;
        }

        public void InsertClasses(Classes classes)
        {
            if ((classes.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(classes, EntityState.Added);
            }
            else
            {
                this.ObjectContext.Classes.AddObject(classes);
            }
        }

        public void UpdateClasses(Classes currentClasses)
        {
            this.ObjectContext.Classes.AttachAsModified(currentClasses, this.ChangeSet.GetOriginal(currentClasses));
        }

        public void DeleteClasses(Classes classes)
        {
            if ((classes.EntityState == EntityState.Detached))
            {
                this.ObjectContext.Classes.Attach(classes);
            }
            this.ObjectContext.Classes.DeleteObject(classes);
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'ClassPresences' query.
        public IQueryable<ClassPresences> GetClassPresences()
        {
            return this.ObjectContext.ClassPresences;
        }

        public void InsertClassPresences(ClassPresences classPresences)
        {
            if ((classPresences.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(classPresences, EntityState.Added);
            }
            else
            {
                this.ObjectContext.ClassPresences.AddObject(classPresences);
            }
        }

        public void UpdateClassPresences(ClassPresences currentClassPresences)
        {
            this.ObjectContext.ClassPresences.AttachAsModified(currentClassPresences, this.ChangeSet.GetOriginal(currentClassPresences));
        }

        public void DeleteClassPresences(ClassPresences classPresences)
        {
            if ((classPresences.EntityState == EntityState.Detached))
            {
                this.ObjectContext.ClassPresences.Attach(classPresences);
            }
            this.ObjectContext.ClassPresences.DeleteObject(classPresences);
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'CourseConfigurations' query.
        public IQueryable<CourseConfigurations> GetCourseConfigurations()
        {
            return this.ObjectContext.CourseConfigurations.Include("Courses");
        }

        public void InsertCourseConfigurations(CourseConfigurations courseConfigurations)
        {
            if ((courseConfigurations.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(courseConfigurations, EntityState.Added);
            }
            else
            {
                this.ObjectContext.CourseConfigurations.AddObject(courseConfigurations);
            }
        }

        public void UpdateCourseConfigurations(CourseConfigurations currentCourseConfigurations)
        {
            this.ObjectContext.CourseConfigurations.AttachAsModified(currentCourseConfigurations, this.ChangeSet.GetOriginal(currentCourseConfigurations));
        }

        public void DeleteCourseConfigurations(CourseConfigurations courseConfigurations)
        {
            if ((courseConfigurations.EntityState == EntityState.Detached))
            {
                this.ObjectContext.CourseConfigurations.Attach(courseConfigurations);
            }
            this.ObjectContext.CourseConfigurations.DeleteObject(courseConfigurations);
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'Courses' query.
        public IQueryable<Courses> GetCourses()
        {
            return this.ObjectContext.Courses;
        }

        public void InsertCourses(Courses courses)
        {
            if ((courses.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(courses, EntityState.Added);
            }
            else
            {
                this.ObjectContext.Courses.AddObject(courses);
            }
        }

        public void UpdateCourses(Courses currentCourses)
        {
            this.ObjectContext.Courses.AttachAsModified(currentCourses, this.ChangeSet.GetOriginal(currentCourses));
        }

        public void DeleteCourses(Courses courses)
        {
            if ((courses.EntityState == EntityState.Detached))
            {
                this.ObjectContext.Courses.Attach(courses);
            }
            this.ObjectContext.Courses.DeleteObject(courses);
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'StudentCourses' query.
        public IQueryable<StudentCourses> GetStudentCourses()
        {
            return this.ObjectContext.StudentCourses;
        }

        public void InsertStudentCourses(StudentCourses studentCourses)
        {
            if ((studentCourses.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(studentCourses, EntityState.Added);
            }
            else
            {
                this.ObjectContext.StudentCourses.AddObject(studentCourses);
            }
        }

        public void UpdateStudentCourses(StudentCourses currentStudentCourses)
        {
            this.ObjectContext.StudentCourses.AttachAsModified(currentStudentCourses, this.ChangeSet.GetOriginal(currentStudentCourses));
        }

        public void DeleteStudentCourses(StudentCourses studentCourses)
        {
            if ((studentCourses.EntityState == EntityState.Detached))
            {
                this.ObjectContext.StudentCourses.Attach(studentCourses);
            }
            this.ObjectContext.StudentCourses.DeleteObject(studentCourses);
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'Students' query.
        public IQueryable<Students> GetStudents()
        {
            return this.ObjectContext.Students;
        }

        public void InsertStudents(Students students)
        {
            if ((students.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(students, EntityState.Added);
            }
            else
            {
                this.ObjectContext.Students.AddObject(students);
            }
        }

        public void UpdateStudents(Students currentStudents)
        {
            this.ObjectContext.Students.AttachAsModified(currentStudents, this.ChangeSet.GetOriginal(currentStudents));
        }

        public void DeleteStudents(Students students)
        {
            if ((students.EntityState == EntityState.Detached))
            {
                this.ObjectContext.Students.Attach(students);
            }
            this.ObjectContext.Students.DeleteObject(students);
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'TutorCourses' query.
        public IQueryable<TutorCourses> GetTutorCourses()
        {
            return this.ObjectContext.TutorCourses;
        }

        public void InsertTutorCourses(TutorCourses tutorCourses)
        {
            if ((tutorCourses.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(tutorCourses, EntityState.Added);
            }
            else
            {
                this.ObjectContext.TutorCourses.AddObject(tutorCourses);
            }
        }

        public void UpdateTutorCourses(TutorCourses currentTutorCourses)
        {
            this.ObjectContext.TutorCourses.AttachAsModified(currentTutorCourses, this.ChangeSet.GetOriginal(currentTutorCourses));
        }

        public void DeleteTutorCourses(TutorCourses tutorCourses)
        {
            if ((tutorCourses.EntityState == EntityState.Detached))
            {
                this.ObjectContext.TutorCourses.Attach(tutorCourses);
            }
            this.ObjectContext.TutorCourses.DeleteObject(tutorCourses);
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'Tutors' query.
        public IQueryable<Tutors> GetTutors()
        {
            return this.ObjectContext.Tutors;
        }

        public void InsertTutors(Tutors tutors)
        {
            if ((tutors.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(tutors, EntityState.Added);
            }
            else
            {
                this.ObjectContext.Tutors.AddObject(tutors);
            }
        }

        public void UpdateTutors(Tutors currentTutors)
        {
            this.ObjectContext.Tutors.AttachAsModified(currentTutors, this.ChangeSet.GetOriginal(currentTutors));
        }

        public void DeleteTutors(Tutors tutors)
        {
            if ((tutors.EntityState == EntityState.Detached))
            {
                this.ObjectContext.Tutors.Attach(tutors);
            }
            this.ObjectContext.Tutors.DeleteObject(tutors);
        }

        public IEnumerable<Courses> GetOnlyOwnerCoursesForCurrentUser()
        {
            MembershipUser mu = Membership.GetUser();
            Guid userId = Guid.Empty;
            if (mu != null)
                userId = (Guid)mu.ProviderUserKey;
            if (userId == Guid.Empty)
                return Enumerable.Empty<Courses>();
            var courses = from tutorCourse in ObjectContext.TutorCourses
                          where tutorCourse.TutorId == userId && tutorCourse.IsOwner
                          select tutorCourse.Courses;
            return courses.ToList();
        }


        public IEnumerable<Courses> GetReadOnlyCoursesForCurrentUser()
        {
            MembershipUser mu = Membership.GetUser();
            Guid userId = Guid.Empty;
            if (mu != null)
                userId = (Guid)mu.ProviderUserKey;
            if (userId == Guid.Empty)
                return Enumerable.Empty<Courses>();
            var courses = from tutorCourse in ObjectContext.TutorCourses
                          where tutorCourse.TutorId == userId && !tutorCourse.IsOwner
                          select tutorCourse.Courses;
            return courses.ToList();
        }


        public IEnumerable<Courses> GetAllCoursesForCurrentUser()
        {
            MembershipUser mu = Membership.GetUser();
            Guid userId = Guid.Empty;
            if (mu != null)
                userId = (Guid)mu.ProviderUserKey;
            if (userId == Guid.Empty)
                return Enumerable.Empty<Courses>();
            var courses = from tutorCourse in ObjectContext.TutorCourses
                          where tutorCourse.TutorId == userId
                          select tutorCourse.Courses;
            return courses.ToList();
        }

        public IQueryable<Students> GetStudentsWithCourses()
        {
            return this.ObjectContext.Students.Include("StudentCourses.Courses");
        }
    }
}


